
---- 次元魔珠 中心服 
local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local MoZhuDB = require("mozhu.MoZhuDB")
local MoZhuExcel = require("excel.mozhu")
local InnerMsg = require("core.InnerMsg")
local MoZhuLogic = require("mozhu.MoZhuLogic")
local CombatDefine = require("combat.CombatDefine")
local Msg = require("core.Msg")
local MiddleManager = require("middle.MiddleManager")

function onHour(hour)

    if _G.is_middle ~= true then return end
    local mozhuDB = MoZhuDB.getMoZhu()
	if not mozhuDB.time then
        MoZhuDB.initMoZhu()
        mozhuDB = MoZhuDB.getMoZhu()
    end
    
    local nowWeek = Util.getWeekDay()
    if not Util.isSameDay(mozhuDB.time) then
        MoZhuDB.initMoZhu()
        --[[   发奖修改为 22点发
        if MoZhuLogic.isOpen() then
            MoZhuDB.initMoZhu()
        else
            local lastWeek = nowWeek - 1
            lastWeek = lastWeek ~= 0 and lastWeek or 7

            if MoZhuLogic.MOZHU_OPEN_DAY[lastWeek] then
                -- 发放上一日 排名奖励
                actOverUnion()
                actOverRole()
                MoZhuDB.initMoZhu()
            else
                MoZhuDB.initMoZhu()
            end
        end
        ]]
        
    end

    if MoZhuLogic.MOZHU_OPEN_DAY[nowWeek] then
        local _, endTime = MoZhuLogic.getActTime()
        local lastGetTime = mozhuDB.lastGetTime or 0
        local now = os.time()
        if now >= endTime and not Util.isSameDay(lastGetTime) then
            mozhuDB.lastGetTime = now 
            -- 发放上一日 排名奖励
            actOverUnion()
            actOverRole()
            saveMoZhu()
        end
    end
end

function getCiTiao()
   local mozhuDB = MoZhuDB.getMoZhu()
   return mozhuDB.citiao
end

function fontRoleRank(mozhuDB, msgInner, topRank)
    local maxRank = topRank and topRank or #mozhuDB.roleRank
    maxRank = maxRank <= #mozhuDB.roleRank and maxRank or #mozhuDB.roleRank
    maxRank = maxRank <= 100  and maxRank or  100
    for i = 1, maxRank do
        local uuid = mozhuDB.roleRank[i]
        local role = mozhuDB.role[uuid]
        if uuid and role then
         --   local net = msgInner.roleRank[i]
            msgInner.roleRank[i] = {}
            msgInner.roleRank[i].uuid = uuid
            msgInner.roleRank[i].roleBase = role.roleBase
            msgInner.roleRank[i].hurt = role.hurt
        end       
    end
end

function fontUnionRank(mozhuDB, msgInner, topRank)
    local maxRank = topRank and topRank or #mozhuDB.unionRank
    maxRank = maxRank <= #mozhuDB.unionRank and maxRank or #mozhuDB.unionRank
    maxRank = maxRank <= 50  and maxRank or 50
    for i = 1, maxRank do
        local unionUuid = mozhuDB.unionRank[i]
        local union = mozhuDB.union[unionUuid]
        if unionUuid and union then
      --      local net = msgInner.unionRank[i]
            msgInner.unionRank[i] = {}
            msgInner.unionRank[i].unionUuid = unionUuid
            msgInner.unionRank[i].unionBase = union.unionBase
            msgInner.unionRank[i].hurt = union.hurt 
        end
    end
end

function fontMyRank(mozhuDB, uuid, msgInner)
    local rank = MoZhuDB.getRoleRank(uuid)
    local role = mozhuDB.role[uuid]
    if rank > 0 and role then
        msgInner.info.myRank.uuid = uuid
        msgInner.info.myRank.rank = rank
        msgInner.info.myRank.roleBase = role.roleBase
        msgInner.info.myRank.hurt = role.hurt
    end
end

function fontMyUnion(mozhuDB, unionUuid, msgInner)
    local union = mozhuDB.union[unionUuid]
    local rank = MoZhuDB.getUnionRank(unionUuid)
    if rank > 0 and union then
        msgInner.info.myUnion.unionUuid = unionUuid
        msgInner.info.myUnion.rank = rank
        msgInner.info.myUnion.unionBase = union.unionBase
        msgInner.info.myUnion.hurt = union.hurt 
    end
end

function  LW_MOZHU_QUERY(fd, msg)

    MoZhuDB.checkUnion()
    local mozhuDB = MoZhuDB.getMoZhu()

    if not mozhuDB or not mozhuDB.time then
 --       MoZhuDB.initMoZhu()
  --      mozhuDB  = MoZhuDB.getMoZhu()
    end
    
   -- Util.printTable(mozhuDB)
    local msgInner = InnerMsg.wl.WL_MOZHU_QUERY
    msgInner.uuid = msg.uuid
    msgInner.myRank = 0
    msgInner.roleRank = {}
    msgInner.unionRank = {}
    msgInner.info = {}
    msgInner.info.citiao = {}
    msgInner.info.myRank = {}
    msgInner.info.myUnion = {}
    msgInner.info.citiao = mozhuDB.citiao
    msgInner.info.citiaoTime = mozhuDB.citiaoTime
    fontUnionRank(mozhuDB, msgInner, 10)
    fontRoleRank(mozhuDB, msgInner, 10)
    fontMyRank(mozhuDB, msg.uuid, msgInner)
    fontMyUnion(mozhuDB, msg.unionUuid, msgInner)
    InnerMsg.sendMsg(fd, msgInner)

end


function WL_MOZHU_QUERY(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if human == nil then
        return  
    end
    MoZhuLogic.WLQuery(human,msg)
end


function LW_MOZHU_FIGHT_END(fd, msg)
    -- 检测活动是否已经结束
    local combatInfo = msg.combatInfo
    local atkHurt = 0
	for pos = 1, CombatDefine.COMBAT_HERO_CNT do
		local obj = combatInfo.objList and combatInfo.objList[pos]
		if obj then
			atkHurt = atkHurt + obj.result[1]
		end
	end

    -- 魔兽 造成伤害
    for _,pos in ipairs(CombatDefine.SIDE2HELPPOS[CombatDefine.ATTACK_SIDE]) do
		local pet = combatInfo.helpList and combatInfo.helpList[pos]
		if pet and pet.isPet then
            atkHurt = atkHurt + pet.result[1]
		end
	end

    if atkHurt <= 0 then
        atkHurt = 0
    end

    local startTime, endTime  = MoZhuLogic.getActTime()
    local now = os.time()
    local msgInner =  InnerMsg.wl.WL_MOZHU_FIGHT_END
    if now < startTime or now > endTime then
        local rank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
        local unRank = MoZhuDB.getUnionRank(msg.unionUuid)
        msgInner.backType = 0
        msgInner.uuid = msg.uuid
        msgInner.oldRank= rank
        msgInner.rank = rank
        msgInner.unionRank = unRank 
        msgInner.unionRanked = unRank
        msgInner.thisHurt = atkHurt
        msgInner.maxHurt = maxHurt
        msgInner.combatInfo = msg.combatInfo
    --  Msg.trace(msgInner)
        InnerMsg.sendMsg(fd, msgInner)
        return 
    end   

    local oldRank = MoZhuDB.getRoleRank(msg.uuid)
    local oldUnRank = MoZhuDB.getUnionRank(msg.unionUuid)
    MoZhuDB.updateHurt(msg.uuid, msg.roleBase, msg.maxHurt)
    MoZhuDB.updateUnion(msg.unionUuid, msg.unionBase)
    

    local newRank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
    local newUnRank = MoZhuDB.getUnionRank(msg.unionUuid)

  --  Util.printTable(MoZhuDB.getMoZhu())
    msgInner.backType = 1
    msgInner.uuid = msg.uuid
    msgInner.oldRank= oldRank
    msgInner.rank = newRank
    msgInner.unionRank = newUnRank 
    msgInner.unionRanked = oldUnRank
    msgInner.thisHurt = atkHurt
    msgInner.maxHurt = maxHurt
    msgInner.combatInfo = msg.combatInfo
  --  Msg.trace(msgInner)
    InnerMsg.sendMsg(fd, msgInner)

    MoZhuDB.saveMoZhu()
end

function WL_MOZHU_FIGHT_END(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if human == nil then
        return  
    end
    MoZhuLogic.onfightEndWL(human,msg)
end


function LW_MOZHU_RANK_QUERY(fd, msg)
    local mozhuDB = MoZhuDB.getMoZhu()
    local msgInner = InnerMsg.wl.WL_MOZHU_RANK_QUERY
    msgInner.uuid = msg.uuid
    msgInner.type = msg.type
    msgInner.roleRank = {}
    msgInner.unionRank = {}
    if msg.type == 1 then
        fontRoleRank(mozhuDB, msgInner)
    else
        fontUnionRank(mozhuDB, msgInner)       
    end
  --  Util.printTable(msgInner)
    InnerMsg.sendMsg(fd, msgInner)
end


function WL_MOZHU_RANK_QUERY(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if human == nil then
        return  
    end

    MoZhuLogic.rankQueryWL(human, msg)
end


function LW_MOZHU_UNION_GET(fd, msg)

    local mozhuDB = MoZhuDB.getMoZhu()
    local union = mozhuDB.union[msg.unionUuid]
    local rank = MoZhuDB.getUnionRank(msg.unionUuid)
    local ret = 0
    if rank > 0 and union then
        local config = MoZhuExcel.hurt[msg.id]
        if config.unionHurt <= union.hurt then
            ret = 1
        end
    end

    local msgInner = InnerMsg.wl.WL_MOZHU_UNION_GET
    msgInner.uuid = msg.uuid
    msgInner.ret  = ret
    msgInner.id   = msg.id
    InnerMsg.sendMsg(fd, msgInner)
end

function WL_MOZHU_UNION_GET(fd, msg)
    local human = ObjHuman.onlineUuid[msg.uuid]
    if human == nil then
        return  
    end

    MoZhuLogic.unionGetWL(human, msg)
end



function LW_ROLE_UNION_OP(fd, msg)
    if msg.op == 1 then
        roleLeaveUnion(msg.uuid, msg.unionUuid)
    elseif msg.op == 2 then
        joinUnion(msg.uuid, msg.unionUuid)
    elseif msg.op == 3 then
        dismissUnion(msg.uuid, msg.unionUuid)
    end
end

function roleLeaveUnion(uuid, unionUuid)
    local mozhuDB = MoZhuDB.getMoZhu()
    local rank = MoZhuDB.getRoleRank(uuid)
    local role = mozhuDB.role[uuid]
    local union = mozhuDB.union[unionUuid]
    if rank > 0 and role then
        MoZhuDB.chageRoleUnionUuid(uuid, "")
        if union then
            MoZhuDB.refreshUnion(unionUuid)           
        end
        MoZhuDB.saveMoZhu()
    end
end

function joinUnion(uuid, unionUuid)
    local mozhuDB = MoZhuDB.getMoZhu()
    local rank = MoZhuDB.getRoleRank(uuid)
    local role = mozhuDB.role[uuid]
    local union = mozhuDB.union[unionUuid]
    if rank > 0 and role then
        MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
        if union then
            MoZhuDB.refreshUnion(unionUuid)             
        end
        MoZhuDB.saveMoZhu()
    end   
end

function dismissUnion(uuid, unionUuid)
    local mozhuDB = MoZhuDB.getMoZhu()
    local rank = MoZhuDB.getRoleRank(uuid)
    local role = mozhuDB.role[uuid]
    if rank > 0 and role then
        MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
    end
    
    local union = mozhuDB.union[unionUuid]
    if union then
        MoZhuDB.dismissUnion(unionUuid)
    end
end
-------------------------------- Act   End  --------------------------

function actOverUnion()
    local mozhuDB = MoZhuDB.getMoZhu()

    local svrList = {}

    for rank, unionUuid in ipairs(mozhuDB.unionRank) do
        local union = mozhuDB.union[unionUuid]
        if unionUuid and union and union.unionBase and union.unionBase.svrIndex and union.hurt > 0 then
            local svrIndex = union.unionBase.svrIndex
            svrList[svrIndex] = svrList[svrIndex] or {}
            svrList[svrIndex][unionUuid] = rank
        end
    end

    for svrIndex, list in pairs(svrList) do
        local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
        msgInner.type = 2
        msgInner.list = msgInner.list or {}
        Util.cleanTable(msgInner.list)
        msgInner.list = list
        local fd = MiddleManager.getFDBySvrIndex(svrIndex)
        InnerMsg.sendMsg(fd, msgInner)
    end
end

function actOverRole()
    local mozhuDB = MoZhuDB.getMoZhu()

    local svrList = {}
    
    for rank, uuid in ipairs(mozhuDB.roleRank) do
        local role = mozhuDB.role[uuid]
        if uuid and role and role.roleBase and role.roleBase.svrIndex and role.hurt > 0 then
            local svrIndex = role.roleBase.svrIndex
            svrList[svrIndex] = svrList[svrIndex] or {}
            svrList[svrIndex][uuid] = rank
        end
    end

    for svrIndex, list in pairs(svrList) do
        local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
        msgInner.type = 1
        msgInner.list = msgInner.list or {}
        Util.cleanTable(msgInner.list)
        msgInner.list = list
        local fd = MiddleManager.getFDBySvrIndex(svrIndex)
        InnerMsg.sendMsg(fd, msgInner)
    end
end

function initAfterHotClean()
    if _G.is_middle == true then
    
        MoZhuDB.refreshAllUnion()
    end

    --[[
local uuidList = {}
    uuidList[1] = "61512a8f639f62538000641a"
    uuidList[2] = "616a5468d98e7c2e9b000ad8"
    uuidList[3] = "616a2005cdd131276d0003cc"
    uuidList[4] = "6160766fcdd1317fc1000065"

    if _G.is_middle ~= true then
        for _, uuid in ipairs(uuidList) do
            local human = ObjHuman.onlineUuid[uuid]
            if human then
                human.db.mozhu = {}
                human.db.mozhu.time = os.time()
                human.db.mozhu.hurtMax = 0
                human.db.mozhu.hurt = 0
                human.db.mozhu.tzCnt = 0
                human.db.mozhu.restCnt = 0
            else
                local RoleDBLogic = require("role.RoleDBLogic")
                local db = RoleDBLogic.getDb(uuid)
                if db then
                    human = {}
                    human.db = db
                    human.db.mozhu = {}
                    human.db.mozhu.time = os.time()
                    human.db.mozhu.hurtMax = 0
                    human.db.mozhu.hurt = 0
                    human.db.mozhu.tzCnt = 0
                    human.db.mozhu.restCnt = 0
                    ObjHuman.save(human)
		        end		
            end
        end
    else
       for _, uuid in ipairs(uuidList) do
           MoZhuDB.delRole(uuid)
       end
    end
        
    ]]
    
end
